package com.heyi.nettylong.client.config;

import com.heyi.nettylong.client.NettyClient;
import com.heyi.nettylong.client.listener.ChannelDataListener;
import com.heyi.nettylong.common.util.SpringBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 客户端初始化配置
 * @author dengfeng
 * @date 2022/4/213:55
 */
@Configuration
@EnableConfigurationProperties(ClientProperties.class)
@ConditionalOnProperty(prefix = "netty.client", value = "enabled", matchIfMissing = true)
public class ClientPropertiesConfig {

    @Autowired
    ClientProperties clientProperties;

    @Bean
    public SpringBeanFactory getHandlerContext() {
        return new SpringBeanFactory();
    }

    @Bean
    public NettyClient getNettyClient(ChannelDataListener channelDataListener, SpringBeanFactory springBeanFactory){
        NettyClient nettyClient = new NettyClient(clientProperties);
        nettyClient.start();
        System.out.println("--------NettyClient已经启动线程---------"+clientProperties.getServerHost()+ ":" + clientProperties.getServerPort());
        return nettyClient;
    }
}
